Component({
  data: {
    topicData: null
  },
  properties: {
    topic: {
      type: Object,
      value: null,
      observer: "__topicObserver"
    },
    title: {
      type: String,
      vlaue: ''
    },
    icon: {
      type: String,
      value: ''
    },
    prop: {
      type: String,
      value: ''
    }
  },
  methods: {
    __topicObserver(val) {
       this.setData({
         topicData: this.packTopic(this.data.prop, val)
       })
    },
    /**
     * 对知识点(单词)的联想和拆分文本拆分
     *
     * @param {Object} topic
     * @returns
     */
    packTopic( prop, topic ) {
      let obj = this.textSplit( topic[prop] );
      if (obj) {
        topic[prop + 'Arr'] = obj.arr;
        topic[prop + 'Poses'] = obj.positions;
      }
      return topic
    },
    textSplit( text ) {
      let matchs = text.match(/(~.*?~)/g);
      if (!matchs) {
        return null;
      }
      let arr = text.replace(/~.*?~/g, '@~@').split('@~@');
      let value = ''
      let flag = 0
      let positions = [];
      let res = matchs.forEach((val, index) => {
        val = val.replace(/~/g, '');
        let pos = index + 1 + flag;
        positions.push(pos);
        arr.splice(pos, 0, val);
        flag ++;
      })
      return {
        positions: positions,
        arr: arr
      }
    }
  }
})